package command;

import screen.*;
import language.*;
import misc.*;
import fs.*;
import apps.edit.*;

public class CommandExecutor {
    public static void run(Screen scr, String s) {
        s = s.trim();
        if (s.equals("exit")) {
            System.exit(0);
        } else if (s.equals("ver")) {
            scr.println("AirplanDOS v1.0");
        } else if (s.startsWith("setlang ")) {
            s = s.substring(8, s.length());
            if (s.equals("English")) LangEnv.setLangEnv(new LangEnv.English());
            else if (s.equals("Chinese")) LangEnv.setLangEnv(new LangEnv.Chinese());
            else scr.println(LangEnv.invalidParameter);
            scr.clear();
            scr.println("AirplanDOS 1.0 (" + s + ")");
            scr.println("");
        } else if (s.equals("dir") || s.equals("ls")) {
            String[] files = FileProc.fileList();
            for (String file: files) {
                scr.println(file);
            }
        } else if (s.startsWith("mkfile ")) {
            s = s.substring(7, s.length());
            if (!FileProc.createFile(s)) scr.println(LangEnv.createFailure);
            else scr.println(LangEnv.createSuccess);
        } else if (s.startsWith("type ")) {
            s = s.substring(5, s.length());
            String[] content = FileProc.getContent(s);
            for (String line : content) scr.println(line);
        } else if (s.startsWith("echo ")) {
            s = s.substring(5, s.length());
            scr.println(s);
        } else if (s.equals("cls")) {
            scr.clear();
        } else if (s.startsWith("rm ")) {
            s = s.substring(3, s.length());
            if (!FileProc.deleteFile(s)) scr.println(LangEnv.deleteFailure);
            else scr.println(LangEnv.deleteSuccess);
        } else if (s.equals("help")) {
            String helpStrs[] =  new String[] {
                LangEnv.helpPrompt,
                "---------------------------------------------------------------------------",
                "help                      - " + LangEnv.helpUse,
                "exit                      - " + LangEnv.exitUse,
                "ver                       - " + LangEnv.verUse,
                "setlang {Chinese|English} - " + LangEnv.setLangUse,
                "dir                       - " + LangEnv.dirLsUse,
                "ls                        - " + LangEnv.dirLsUse,
                "mkfile {filename}         - " + LangEnv.mkfileUse,
                "type {filename}           - " + LangEnv.typeUse,
                "echo {string}             - " + LangEnv.echoUse,
                "cls                       - " + LangEnv.clsUse,
                "rm {filename}             - " + LangEnv.rmUse,
                "---------------------------------------------------------------------------"
            };
            for (String helpStr : helpStrs) {
                scr.println(helpStr);
            }
        } else {
            if (s.startsWith("edit ")) Edit.planeMain(parseCommand(s, "edit"));
            else if (s.equals("edit")) Edit.planeMain(new String[] {});
            else if (s.equals("")) return;
            else scr.println(LangEnv.badCommand);
        }
    }
    public static String[] parseCommand(String command, String commandName) {
        return command.substring((commandName + " ").length(), command.length()).split(" ");
    }
}